package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.ProductionLine;
import com.efast.dmom.aps.dao.ProductionLineDao;
import com.efast.dmom.aps.entity.ProductionLineEntity;
import com.efast.dmom.aps.util.TimeUtil;

@Component
public class ProductionLineDataHandler {
	
	@Autowired
	private ProductionLineDao productionLineDao;

	public int getProdLineCapacity(long pLineKey) {
		return productionLineDao.getProdLineCapacity(pLineKey);
	}

	public void save(ProductionLine obj) {

		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//			obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(111);
			productionLineDao.save(obj.getInnerData());
		}
	}

	public void update(ProductionLine obj) {

		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			productionLineDao.update(obj.getInnerData());
		}
	}

	public void delete(ProductionLine obj) {
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			productionLineDao.delete(obj.getInnerData());
		}
	}

	public ProductionLine getById(long id) {
		ProductionLine productionLine = new ProductionLine();
		productionLine.setInnerData(productionLineDao.getById(id));
		return productionLine;
	}

	public ProductionLine getByName(String name) {
		ProductionLine productionLine = new ProductionLine();
		productionLine.setInnerData(productionLineDao.getByName(name));
		return productionLine;
	}


	public ProductionLine getByCode(String productionLineCode) {
		ProductionLine productionLine = new ProductionLine();
		productionLine.setInnerData(productionLineDao.getByCode(productionLineCode));
		return productionLine;
	}
	
	public int getProductionLineById(Long id) {
		int productionLineById = productionLineDao.getProductionLineById(id);
		return productionLineById;
		
	}

	public List<ProductionLine> getAllProductionLine() {
		List<ProductionLineEntity> entityList = ((ProductionLineDao) productionLineDao).getAllProductionLine();
		List<ProductionLine> productionLineList = new ArrayList<ProductionLine>();
		if (entityList != null && entityList.size() > 0) {
		for (ProductionLineEntity entityData:entityList) {
			ProductionLine productionLine=new ProductionLine();
			productionLine.setInnerData(entityData);
			productionLineList.add(productionLine);
		}
		return productionLineList;
		}
		return null;
	}
	
	public List<Map<String,String>> queryCodeIdNameByWorkshopId(Long workshopId) {
		return productionLineDao.queryCodeIdNameByWorkshopId(workshopId);
	}
	
	}

